﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Ryder_US_Website.BUS;

namespace Ryder_US_Website.Presentation.Admin
{
    public partial class Service_List : System.Web.UI.Page
    {
        private String []arr_trash = new String[2]{"Hoạt động","Không hoạt động"};
        private String ms = "Chưa có mô tả nào";
        List<RUS_Service> list = new List<RUS_Service>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["RUS_User"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            list = ServiceBUS.GetAll();
            DataTable dtb = new DataTable();
            dtb.Columns.Add("#");
            dtb.Columns.Add("Tên dịch vụ");
            dtb.Columns.Add("Mô tả");
            dtb.Columns.Add("Tình trạng");
            DataRow dr = null;
            for(int i=0;i<list.Count;i++)
            {
                dr = dtb.NewRow();
                dr["#"] = (i + 1).ToString();
                dr["Tên dịch vụ"] = "<a href=' Service_Update.aspx?id=" + list[i].Service_ID + "'>" + list[i].Service_name + "</a>";
                if (list[i].Service_description == "")
                {
                    list[i].Service_description = ms;
                    dr["Mô tả"] = list[i].Service_description;
                }
                else
                {
                    dr["Mô tả"] = list[i].Service_description;
                    
                }
                dtb.Rows.Add(dr);
            }
            grvService.DataSource = dtb;
            grvService.DataBind();
        }

        protected void grvService_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = e.Row.RowIndex;
                e.Row.Cells[1].Text = Server.HtmlDecode(e.Row.Cells[1].Text);

                 //Link button xóa
                LinkButton lkbtn = new LinkButton();
                lkbtn.Text = arr_trash[list[i].Service_trash];
                lkbtn.Command += new CommandEventHandler(lkbtnXoa_Click);
                lkbtn.CommandArgument = list[i].Service_ID.ToString();
                e.Row.Cells[3].Controls.Add(lkbtn);
            }
        }
        protected void lkbtnXoa_Click(object sender, CommandEventArgs e)
        {
            try
            {
                ServiceBUS.DeleteRestore(int.Parse(e.CommandArgument.ToString()));
            }
            catch (Exception ex)
            {
              Response.Write(Utils.UtilHelpers.ShowMessage(ex.Message));
            }
            
            Page_Load(sender, e);
        }

        protected void grvService_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvService.PageIndex = e.NewPageIndex;
            grvService.DataBind();

        }
    }
}
